package org.example.netty.dev1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import org.example.netty.dev1.codec.RpcDecoder;
import org.example.netty.dev1.codec.RpcEncoder;
import org.example.netty.dev1.entity.Request;
import org.example.netty.dev1.entity.Response;
import org.example.netty.dev1.handler.NettyClientHandler;
import org.example.netty.dev1.handler.NettyServerHandler;

public class NettyServer {

    private final String ip ;
    private final int port;



    public NettyServer(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    private void serverStar(){
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workGroup = new NioEventLoopGroup();
        try {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,1024)
                .option(ChannelOption.SO_SNDBUF, 32 * 1024)
                .option(ChannelOption.SO_RCVBUF,32 * 1024 )
                .option(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline= ch.pipeline();
                       // pipeline.addLast(new LengthFieldBasedFrameDecoder(10240, 0, 4, 0, 4));
                      //  pipeline.addLast(new LengthFieldPrepender(4));
                        pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
                        pipeline.addLast(new ObjectEncoder());
                        pipeline.addLast(new NettyServerHandler());


                    }
                }).option(ChannelOption.SO_BACKLOG, 128);
            serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
            ChannelFuture future = serverBootstrap.bind(port).sync();
            System.out.println("netty server is staring ....");
            future.channel().closeFuture().sync();
            System.out.println("netty server is stop...");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {

        new NettyServer("127.0.0.1", 5001).serverStar();

    }
}
